from django.db import models

# Create your models here.
from django.db import models


class City(models.Model):
    name = models.CharField(max_length=100, unique=True)

    def __str__(self):
        return self.name


class UserProfile(models.Model):
    USER_TYPE_CHOICES = (
        ('CITY', 'City User'),
        ('NODE', 'Node User'),
    )

    username = models.CharField(max_length=150)
    user_type = models.CharField(max_length=10, choices=USER_TYPE_CHOICES)
    password = models.CharField(max_length=255)
    city = models.ForeignKey(City, null=True, blank=True, on_delete=models.SET_NULL)

    class Meta:
        unique_together = ('username', 'user_type')  # 联合主键

    def __str__(self):
        return f'{self.username} ({self.get_user_type_display()})'

# 新增 NodeData 模型，用于存储 Node 上传的数据
class NodeData(models.Model):
    # 关联到用户，记录哪个用户上传的文件
    user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)

    # Node 的名称，与上传的数据相关联
    node_name = models.CharField(max_length=100)

    # 上传的文件，文件将会存储在 `uploads/YYYY/MM/DD/` 目录下
    file = models.FileField(upload_to='uploads/%Y/%m/%d/')

    # 文件上传时间
    uploaded_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.node_name} - {self.user.username} - {self.file.name}"
